package com.module.ble.ringMonitorService

import android.content.Context
import androidx.work.*
import java.util.concurrent.TimeUnit

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/4/14
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class MonitorServiceChecker(context: Context, workerParams: WorkerParameters)
    : Worker(context, workerParams) {

    override fun doWork(): Result {
        // 检查并恢复应该在后台运行的服务
        RingCusServiceController.checkAndRestoreServices(applicationContext)

        return Result.success()
    }

    companion object {
        // 注册定期检查任务
        fun scheduleServiceCheck(context: Context) {
            val constraints = Constraints.Builder()
                .setRequiresBatteryNotLow(true)
                .build()

            val request = PeriodicWorkRequestBuilder<MonitorServiceChecker>(
                15, TimeUnit.MINUTES)
                .setConstraints(constraints)
                .build()

            WorkManager.getInstance(context)
                .enqueueUniquePeriodicWork(
                    "monitor_service_check",
                    ExistingPeriodicWorkPolicy.REPLACE,
                    request
                )
        }
    }
}